package com.he.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.he.constant.RedisConstant;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.dto.PhotoDTO;
import com.he.domain.dto.PhotoInfoDTO;
import com.he.domain.vo.AlbumBackVO;
import com.he.domain.vo.PhotoBackVO;
import com.he.domain.vo.PhotoVO;
import com.he.enums.FilePathEnum;
import com.he.mapper.AlbumMapper;
import com.he.mapper.PhotoMapper;
import com.he.service.PhotoService;
import com.he.entity.Photo;
import com.he.strategy.context.UploadStrategyContext;
import com.he.utils.BeanCopyUtils;
import com.he.utils.MinioUtils;
import com.he.utils.PageUtils;
import com.he.utils.RedisCache;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/16 11:23
 * @Desc 相片Service实现类
 */
@Service
public class PhotoServiceImpl extends ServiceImpl<PhotoMapper, Photo> implements PhotoService {

    @Resource
    private PhotoMapper photoMapper;

    @Resource
    private AlbumMapper albumMapper;

    @Resource
    private RedisCache redisCache;

    @Resource
    private MinioUtils minioUtils;

    /**
     * 上传策略上下文
     */
    @Resource
    private UploadStrategyContext uploadStrategyContext;

    @Override
    public List<PhotoVO> photoListByAlbumId(Integer albumId) {
        return photoMapper.selectPhotoListByAlbumId(albumId);
    }

    @Override
    public List<PhotoVO> getCarouselsPhotoList() {
        return photoMapper.selectPhotoListByAlbumId(1);
    }

    @Override
    public String uploadPhoto(MultipartFile file) {
        return minioUtils.upload(file);
//        return uploadStrategyContext.executeUploadStrategy(file, FilePathEnum.ALBUM.getPath() + albumId +
//                FilePathEnum.BASE.getPath());
    }

    @Override
    public void addPhotos(List<PhotoDTO> photoDTOList) {
        List<Photo> photoList = photoDTOList.stream().map(
                photoDTO -> Photo.builder().albumId(photoDTO.getAlbumId())
                            .photoUrl(photoDTO.getPhotoUrl())
                            .photoDesc(photoDTO.getPhotoDesc())
                            .build()).collect(Collectors.toList());
        redisCache.deleteObject(RedisConstant.SITE_CONFIG_KEY);
        saveBatch(photoList);
    }

    @Override
    public AlbumBackVO getAlbumInfo(Integer albumId) {
        AlbumBackVO albumBackVO = albumMapper.selectAlbumInfoById(albumId);
        if (Objects.isNull(albumBackVO)) {
            return null;
        }
        Long photoCount = photoMapper.selectCount(new LambdaQueryWrapper<Photo>()
                .eq(Photo::getAlbumId, albumId));
        albumBackVO.setPhotoCount(photoCount);
        return albumBackVO;
    }

    @Override
    public PageResult<PhotoBackVO> listPhotoBackVO(ConditionDTO condition) {
        // 查询照片数量
        Long count = photoMapper.selectCount(new LambdaQueryWrapper<Photo>()
                .eq(Objects.nonNull(condition.getAlbumId()), Photo::getAlbumId, condition.getAlbumId()));
        if (count == 0) {
            return new PageResult<>();
        }
        // 查询照片列表
        List<PhotoBackVO> photoList = photoMapper.selectPhotoBackVOList(PageUtils.getLimit(),
                PageUtils.getSize(), condition.getAlbumId());
        return new PageResult<>(photoList, count);
    }

    @Override
    public void updatePhoto(PhotoInfoDTO photoInfo) {
        Photo photo = BeanCopyUtils.copyBean(photoInfo, Photo.class);
        photo.setPhotoUrl(photoInfo.getPhotoUrl());
        redisCache.deleteObject(RedisConstant.SITE_CONFIG_KEY);
        baseMapper.updateById(photo);
    }

    @Override
    public void deletePhoto(List<Integer> photoIdList) {
        removeByIds(photoIdList);
    }


}
